<template>
  <!--表单组件-->
  <el-dialog :close-on-click-modal="false" :before-close="handleBeforeClose"
    :visible=true :title="title" width="500px">

    <el-form ref="form" :model="form" :rules="rules" size="small" label-width="160px">
      <el-form-item label="平视仪复杂动作判断(s)" prop="psydz">
        <el-input v-model="form.psydz" style="width: 370px;" />
      </el-form-item>
      <el-form-item label="仪表复杂动作判断(s)" prop="ybdz">
        <el-input v-model="form.ybdz" style="width: 370px;" />
      </el-form-item>
      <el-form-item label="自由练习(s)">
        <el-input v-model="form.zylx" style="width: 370px;" />
      </el-form-item>
      <el-form-item label="固定航线飞行(s)">
        <el-input v-model="form.gdhxfx" style="width: 370px;" />
      </el-form-item>
      <el-form-item label="模拟飞行着陆(s)">
        <el-input v-model="form.mnfxzl" style="width: 370px;" />
      </el-form-item>
      <el-form-item label="速度表闪现">
        <el-input v-model="form.sdbsx" style="width: 370px;" />
      </el-form-item>
      <el-form-item label="高度表闪现">
        <el-input v-model="form.gdbsx" style="width: 370px;" />
      </el-form-item>
      <el-form-item label="平视仪闪现">
        <el-input v-model="form.psysx" style="width: 370px;" />
      </el-form-item>
      <el-form-item label="地平仪闪现">
        <el-input v-model="form.dpysx" style="width: 370px;" />
      </el-form-item>
      <el-form-item label="罗盘闪现">
        <el-input v-model="form.lpsx" style="width: 370px;" />
      </el-form-item>
      <el-form-item label="坚信平视仪_航向误差">
        <el-input v-model="form.jspsyHxwc" style="width: 370px;" />
      </el-form-item>
      <el-form-item label="坚信平视仪_高度误差">
        <el-input v-model="form.jspsyGdwc" style="width: 370px;" />
      </el-form-item>
      <el-form-item label="坚信平视仪_附加成绩">
        <el-input v-model="form.jspsyFjcj" style="width: 370px;" />
      </el-form-item>
      <el-form-item label="坚信平视仪_保持时间">
        <el-input v-model="form.jspsyBcsj" style="width: 370px;" />
      </el-form-item>
      <el-form-item label="坚信仪表_航向误差">
        <el-input v-model="form.jsybHxwc" style="width: 370px;" />
      </el-form-item>
      <el-form-item label="坚信仪表_高度误差">
        <el-input v-model="form.jsybGdwc" style="width: 370px;" />
      </el-form-item>
      <el-form-item label="坚信仪表_附加成绩">
        <el-input v-model="form.jsybFjcj" style="width: 370px;" />
      </el-form-item>
      <el-form-item label="坚信仪表_保持时间">
        <el-input v-model="form.jsybBcsj" style="width: 370px;" />
      </el-form-item>
      <el-form-item label="评定结果">
        <el-select
          v-model="form.result"
          placeholder="请选择"
          style="width: 300px;"
        >
          <el-option
            v-for="[key, value] in resultOptions"
            :key="value"
            :label="value"
            :value="key"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="记录人">
        <el-input v-model="form.createBy" style="width: 370px;" />
      </el-form-item>
      <el-form-item label="记录日期">
        <el-date-picker v-model="form.date" :picker-options="pickerOptions" type="date" />

      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
        <el-button type="text" @click="handleCancel">取消</el-button>
        <el-button type="primary" @click="handleCommit" :disabled="disabled">确认</el-button>
      </div>
  </el-dialog>
</template>

<script>
import {get, edit, add} from '@/api/trainFxybdx'

import { RATE_RESULTS} from '@/utils/constants'


const defaultForm = { 
  id: null, trainId: null, trainItemId: null, cycz: null, ajxy: null, hpxyWf: null, 
  hpxy: null, xzdl: null, zxz: null, yxz: null, cxsj: null, hpzw: null, qxtb: null, 
  g: null, gWf: null, result: null, jy: null, createBy: null, date: null }

export default {
  name: 'FxybdxDialog',
  props: {
    trainId: {
      type: Number,
      default: -1,
      required: true
    },
    itemId: {
      type: Number,
      default: -1,
      required: true
    }
  },
  computed: {
    disabled() {
      return this.form.id != null && !this.checkPer(['admin']);
    }
  },
  data() {
    return {
            pickerOptions: {
        shortcuts: [{
          text: '今天',
          onClick(picker) {
            picker.$emit('pick', new Date());
          }
        }]
      },
      form: { ...defaultForm },
      title: '飞行仪表定向',
      rules: {
        createBy: [{ required: true, message: '记录人不能为空', trigger: 'blur' }],
        date: [{ required: true, message: '记录日期不能为空', trigger: 'blur' }]
      },
      resultOptions: RATE_RESULTS,
      isLoading: false // 提交按钮的加载状态
    };
  },
  methods: {
    emit: function(event) {

    },
    handleBeforeClose(done) {
      this.$emit('close', this.itemId);
      if (done) {
        done(); // 调用done()来关闭对话框        
      }
    },
    handleCommit() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          this.isLoading = true; // 提交按钮的加载状态
          this.form.trainId = this.trainId;
          this.form.trainItemId = this.itemId;
          if (this.form.id == null) {
            add(this.form).then(() => {
              this.$emit('close', this.itemId);
              this.isLoading = false;
            }).catch(() => {
              this.isLoading = false;
            });
          } else {
            edit(this.form).then(() => {
              this.$emit('close', this.itemId);
              this.isLoading = false;
            }).catch(() => {
              this.isLoading = false;
            });
          }
        }
      })
    },
    handleCancel() {
      this.$emit('cancel', this.itemId);
    }
  },
  created() {
    console.log(this.trainId, this.itemId);
    if (this.trainId !== -1) {
      this.shouldUpdate = false;

      get(this.trainId, this.itemId).then(response => {
        this.form = Object.assign(this.form, response);
        console.log(response, this.form);

      });
    }
  },
  updated() {

    // 组件更新后执行的逻辑，比如获取数据
  },
  mounted() {

    // if (this.trainId !== -1) {

    //   this.isVisible = 
    // }
    // 组件挂载后执行的逻辑，比如根据props初始化状态
  }
}
</script>

<style scoped>
.el-input, .el-select{
  width: 300px !important;
}

</style>